package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.bean.SedSynCusManageVO;
import com.fdb.basic.framework.core.exception.PauseException;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.service.facade.PrdInfoService;
import com.fdb.efp.nls.service.facade.PubSysInfoService;
import com.fdb.efp.nls.service.vo.PrdInfoVO;
import com.fdb.efp.nls.common.ContConstant;
import com.fdb.efp.nls.service.dao.CtrLoanContDao;
import com.fdb.efp.nls.service.domain.CtrLoanCont;
import com.fdb.efp.nls.service.facade.CtrLoanContService;
import com.fdb.efp.nls.service.vo.CtrLoanContVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 贷款合同表				<p>
 * @author chenqm1
 * @date 2018-05-04
 */
@Service("ctrLoanContService")
public class CtrLoanContServiceImpl implements CtrLoanContService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(CtrLoanContServiceImpl.class);

	@Autowired
	private CtrLoanContDao ctrLoanContDao;

	@Autowired
	@Qualifier("pubSysInfoService")
	private PubSysInfoService pubSysInfoService;

	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;



	/**
	 * 新增操作
	 */
	public int insertCtrLoanCont(CtrLoanContVO inCtrLoanContVo) {
		logger.info("当前新增数据为:"+ JSON.toJSONString(inCtrLoanContVo));
		int num = 0;
		try {
			CtrLoanCont ctrLoanCont = new CtrLoanCont();
			beanCopy(inCtrLoanContVo, ctrLoanCont);
			num = ctrLoanContDao.insertCtrLoanCont(ctrLoanCont);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.info("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(CtrLoanContVO inCtrLoanContVo) {
		logger.debug("当前删除数据条件为:"+ JSON.toJSONString(inCtrLoanContVo));
		int num = 0;
		try {
			CtrLoanCont ctrLoanCont = new CtrLoanCont();
			beanCopy(inCtrLoanContVo, ctrLoanCont);
			num = ctrLoanContDao.deleteByPk(ctrLoanCont);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCtrLoanContVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(CtrLoanContVO inCtrLoanContVo) {
		logger.debug("当前修改数据为:"+ JSON.toJSONString(inCtrLoanContVo));
		int num = 0;
		try {
			CtrLoanCont ctrLoanCont = new CtrLoanCont();
			beanCopy(inCtrLoanContVo, ctrLoanCont);
			ctrLoanCont.setLastUpdateTime(TimeUtil.getCurrentDateTime());
			num = ctrLoanContDao.updateByPk(ctrLoanCont);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCtrLoanContVo +"修改的数据条数为"+ num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public CtrLoanContVO queryByPk(CtrLoanContVO inCtrLoanContVo) {

		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(inCtrLoanContVo));
		try {
			CtrLoanCont queryCtrLoanCont = new CtrLoanCont();
			beanCopy(inCtrLoanContVo, queryCtrLoanCont);
			CtrLoanCont queryRslCtrLoanCont = ctrLoanContDao.queryByPk(queryCtrLoanCont);
			if (Objects.nonNull(queryRslCtrLoanCont)) {
				CtrLoanContVO outCtrLoanContVo = beanCopy(queryRslCtrLoanCont, new CtrLoanContVO());
				logger.debug("当前查询结果为:"+ outCtrLoanContVo.toString());
				return outCtrLoanContVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryAllOwner(CtrLoanContVO ctrLoanContVo) {

		logger.info("当前查询本人所属数据信息的参数信息为:"+JSON.toJSONString(ctrLoanContVo));

		//CtrLoanCont ctrLoanCont = beanCopy(ctrLoanContVo, new CtrLoanCont());
		List<CtrLoanContVO> list = null;
		try {
			List<CtrLoanCont> ctrLoanConts = ctrLoanContDao.queryAllOwnerByPage(ctrLoanContVo);
			logger.info("当前查询本人所属数据信息的结果集数量为:"+ ctrLoanConts.size());
			pageSet(ctrLoanConts, ctrLoanContVo);
			list = (List<CtrLoanContVO>) beansCopy(ctrLoanConts, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryAllCurrOrg(CtrLoanContVO ctrLoanContVo) {
		logger.info("当前查询本人所属机构数据信息的参数信息为:");
		List<CtrLoanContVO> list = null;
		try {
			List<CtrLoanCont> ctrLoanConts = ctrLoanContDao.queryAllCurrOrgByPage(ctrLoanContVo);
			logger.info("当前查询本人所属机构数据信息的结果集数量为:"+ctrLoanConts.size());
			pageSet(ctrLoanConts, ctrLoanContVo);
			list = (List<CtrLoanContVO>) beansCopy(ctrLoanConts, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;

	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryAllCurrDownOrg(CtrLoanContVO ctrLoanContVo) {

		logger.info("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<CtrLoanContVO> list = null;
		try {
			List<CtrLoanCont> ctrLoanConts = ctrLoanContDao.queryAllCurrDownOrgByPage(ctrLoanContVo);
			logger.info("当前查询本人所属机构及以下数据信息的结果集数量为:"+ ctrLoanConts.size());
			pageSet(ctrLoanConts, ctrLoanContVo);
			list = (List<CtrLoanContVO>) beansCopy(ctrLoanConts, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;

	}

	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryAllCurrOwnerPrd(CtrLoanContVO ctrLoanContVo) {

		logger.info("当前查询本人所属产品以下数据信息的参数信息为:");
		List<CtrLoanContVO> list = null;
		try {
			List<CtrLoanCont> ctrLoanConts = ctrLoanContDao.queryAllCurrOwnerPrdByPage(ctrLoanContVo);
			logger.info("当前查询本人所属产品及以下数据信息的结果集数量为:"+ ctrLoanConts.size());
			pageSet(ctrLoanConts, ctrLoanContVo);
			list = (List<CtrLoanContVO>) beansCopy(ctrLoanConts, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;

	}

	/**
	 * 条件查询
	 *
	 */
	public CtrLoanContVO queryByCondition(CtrLoanContVO inCtrLoanContVo) {
		try {
			CtrLoanCont queryCtrLoanCont = new CtrLoanCont();
			queryCtrLoanCont = (CtrLoanCont)beanCopy(inCtrLoanContVo, queryCtrLoanCont);
			logger.info("当前查询参数信息为:"+ JSON.toJSONString(queryCtrLoanCont));
			CtrLoanCont queryRslCtrLoanCont = ctrLoanContDao.queryByCondition(queryCtrLoanCont);
			if (Objects.nonNull(queryRslCtrLoanCont)) {
				CtrLoanContVO outCtrLoanContVo = beanCopy(queryRslCtrLoanCont, new CtrLoanContVO());
				logger.info("当前查询结果为:"+ outCtrLoanContVo.toString());
				return outCtrLoanContVo;
			} else {
				logger.info("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	/**
	 * 列表条件查询
	 * @throws Exception
	 *
	 * */

	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryListByCondition(CtrLoanContVO inCtrLoanContVo) throws Exception {
		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(inCtrLoanContVo));
		if(Objects.isNull(inCtrLoanContVo)) {
			return null;
		}
		try {
			List<CtrLoanContVO> list = null;
			List<CtrLoanCont> queryListByCondition = ctrLoanContDao.queryListByCondition(inCtrLoanContVo);
			logger.info("当前查询结果为:"+JSON.toJSONString(queryListByCondition));
			if (Objects.nonNull(queryListByCondition)&&queryListByCondition.size()!=0) {
				list = (List<CtrLoanContVO>) beansCopy(queryListByCondition, CtrLoanContVO.class);
				pageSet(list, inCtrLoanContVo);
				return list;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
			throw e;
		}
		return null;
	}
	/**
	 * 生效  已出账合同列表查询
	 *
	 * */

	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryFrozenListByCondition(CtrLoanContVO inCtrLoanContVo) {
		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(inCtrLoanContVo));
		if(Objects.isNull(inCtrLoanContVo)) {
			return null;
		}
		try {
			List<CtrLoanContVO> list = new ArrayList<>();
			List<CtrLoanCont> queryListByCondition = ctrLoanContDao.queryFrozenListByConditionByPage(inCtrLoanContVo);
			logger.info("当前查询参数为:"+JSON.toJSONString(inCtrLoanContVo));
			logger.info("当前查询结果为:"+JSON.toJSONString(queryListByCondition));
			if (Objects.nonNull(queryListByCondition)&&queryListByCondition.size()!=0) {
				list = (List<CtrLoanContVO>) beansCopy(queryListByCondition, CtrLoanContVO.class);
				pageSet(list, inCtrLoanContVo);
				return list;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<CtrLoanContVO> queryListBySemiClose(CtrLoanContVO inCtrLoanContVo) {
		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(inCtrLoanContVo));
		try {
			List<CtrLoanContVO> list=null;
			CtrLoanCont queryCtrLoanCont = new CtrLoanCont();
			//beanCopy(inCtrLoanContVo, queryCtrLoanCont);
			List<CtrLoanCont> queryListByCondition = ctrLoanContDao.queryListBySemiCloseByPage(inCtrLoanContVo);
			logger.info("当前查询结果为:"+JSON.toJSONString(queryListByCondition));
			if (Objects.nonNull(queryListByCondition)&&queryListByCondition.size()!=0) {
				pageSet(queryListByCondition, queryCtrLoanCont);
				list = (List<CtrLoanContVO>) beansCopy(queryListByCondition, CtrLoanContVO.class);
				return list;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public int updateByContNo(String contNo ,String loanAccount) {
		//更新成功标注
		int flag=1;
		logger.debug("当前更新参数信息为:"+ contNo);
		CtrLoanContVO inCtrLoanContVo= new CtrLoanContVO();
		inCtrLoanContVo.setContNo(contNo);
		inCtrLoanContVo.setLoanAccount(loanAccount);
		int updateByPk = updateByPk(inCtrLoanContVo);
		if(updateByPk!=1) {
			flag=0;
			logger.debug("更新失败，请处理!");
		}
		return flag;
	}
	@Override
	public boolean signCtrLoanCont(String contNo) throws Exception {
		logger.info("开始签订借款合同，contNo="+contNo);
		try {
			CtrLoanCont ctrLoanCont = new CtrLoanCont();
			ctrLoanCont.setContNo(contNo);//合同号
			CtrLoanCont ctrLoanContFromDB = ctrLoanContDao.queryByPk(ctrLoanCont);
			if(Objects.nonNull(ctrLoanContFromDB)) {
				//生成合同pdf已确定了合同起始日等日期，此处不修改 mod by 20180628
                /*String openday = pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001);//营业日期
                int term = ctrLoanContFromDB.getLoanTerm().intValue();
                String termType = ctrLoanContFromDB.getTermTimeType();
                
                String loanEndDate = openday;
                if(BaseConstant.TERM_TYPE_DAY.equals(termType)) {
                    loanEndDate = TimeUtil.addDay(openday, term);
                } else if(BaseConstant.TERM_TYPE_MONTH.equals(termType)) {
                    loanEndDate = TimeUtil.addMonth(openday, term);
                } else if(BaseConstant.TERM_TYPE_YEAR.equals(termType)) {
                    loanEndDate = TimeUtil.addYear(openday, term);
                }
                
                ctrLoanCont.setSignDate(openday);//签订日期
                ctrLoanCont.setLoanStartDate(openday);//合同起始日期
                ctrLoanCont.setLoanEndDate(loanEndDate);//合同到期日期
*/                ctrLoanCont.setContState(ContConstant.CONT_STATUS_200);//合同状态
				//ctrLoanCont.setLastUpdateUser(lastUpdateUser);//最后修改人
				ctrLoanCont.setLastUpdateTime(TimeUtil.getCurrentDateTime());//最后修改时间

				int count = ctrLoanContDao.updateByPk(ctrLoanCont);
				logger.info(contNo+"签订借款合同结果：" + (count==1?"success":"failure"));
				if(count!=1) {
					throw new PauseException("签订借款合同失败，合同号：" + contNo);
				}
			} else {
				logger.error("根据合同号无法获取到借款合同信息，合同号：" + contNo);
				throw new PauseException("根据合同号无法获取到借款合同信息，合同号：" + contNo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return true;
	}

	@Override
	public boolean updateLoanAccount(String contNo, String loanAccount) throws Exception {
		logger.info("开始更新合同贷款账号，contNo="+contNo+"，loanAccount="+loanAccount);
		try {
			CtrLoanCont ctrLoanCont = new CtrLoanCont();
			ctrLoanCont.setContNo(contNo);//合同号
			ctrLoanCont.setLoanAccount(loanAccount);//贷款账号
			//ctrLoanCont.setContState(ContConstant.CONT_STATUS_800);//合同状态
			//ctrLoanCont.setLastUpdateUser(lastUpdateUser);//最后修改人
			ctrLoanCont.setLastUpdateTime(TimeUtil.getCurrentDateTime());//最后修改时间

			int count = ctrLoanContDao.updateByPk(ctrLoanCont);
			logger.info(contNo+"更新合同贷款账号结果：" + (count==1?"success":"failure"));
			if(count!=1) {
				throw new PauseException("更新合同贷款账号失败，合同号：" + contNo + "，贷款账号：" + loanAccount);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return true;
	}

	@Override
	public List<CtrLoanContVO> queryCtrLoanContListByContNos(List<String> contNos) {
		logger.info("根据合同号列表查询合同信息列表，入参："+JSONObject.toJSONString(contNos));
		if(Objects.isNull(contNos)) {
			return null;
		}
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();

		List<CtrLoanCont> list = ctrLoanContDao.queryCtrLoanContListByContNos(contNos);
		if(Objects.nonNull(list)) {
			for(CtrLoanCont cont : list) {
				CtrLoanContVO contVO = new CtrLoanContVO();
				beanCopy(cont, contVO);
				retList.add(contVO);
			}
		}
		return retList;
	}

	@Override
	public List<String> queryContNoByLmtContNo(String lmtContNo) {
		return ctrLoanContDao.queryContNoByLmtContNo(lmtContNo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CtrLoanContVO> queryByConditionByPage(CtrLoanContVO ctrLoanContVO) {
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();
		try {
			List<CtrLoanCont> list = ctrLoanContDao.queryByConditionByPage(ctrLoanContVO);
			if(Objects.nonNull(list)) {
				for(CtrLoanCont cont : list) {
					CtrLoanContVO contVO = new CtrLoanContVO();
					beanCopy(cont, contVO);
					retList.add(contVO);
				}
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ retList.size());
			pageSet(retList, ctrLoanContVO);
			list = (List<CtrLoanCont>) beansCopy(retList, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return retList;
	}

	/**
	 * 查询用户权限数据 ——专项检查 
	 */
	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryByOwner(CtrLoanContVO ctrLoanContVO) {

		logger.info("当前查询本人所属数据信息的参数信息为:"+JSON.toJSONString(ctrLoanContVO));
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();
		try {
			List<CtrLoanCont> list = ctrLoanContDao.queryByOwnerByPage(ctrLoanContVO);
			if(Objects.nonNull(list)) {
				for(CtrLoanCont cont : list) {
					CtrLoanContVO contVO = new CtrLoanContVO();
					beanCopy(cont, contVO);
					retList.add(contVO);
				}
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ retList.size());
			pageSet(retList, ctrLoanContVO);
			list = (List<CtrLoanCont>) beansCopy(retList, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return retList;
	}
	/**
	 * 查询当前机构权限数据——专项检查 
	 */
	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryByCurrOrg(CtrLoanContVO ctrLoanContVO) {

		logger.info("当前查询本人所属机构数据信息的参数信息为:"+JSONObject.toJSONString(ctrLoanContVO));
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();
		try {
			List<CtrLoanCont> list = ctrLoanContDao.queryByCurrOrgByPage(ctrLoanContVO);
			if(Objects.nonNull(list)) {
				for(CtrLoanCont cont : list) {
					CtrLoanContVO contVO = new CtrLoanContVO();
					beanCopy(cont, contVO);
					retList.add(contVO);
				}
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ retList.size());
			pageSet(retList, ctrLoanContVO);
			list = (List<CtrLoanCont>) beansCopy(retList, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return retList;
	}
	/**
	 * 查询当前机构及下属机构权限数据——专项检查 
	 */
	@SuppressWarnings("unchecked")
	public List<CtrLoanContVO> queryByCurrDownOrg(CtrLoanContVO ctrLoanContVO) {

		logger.info("当前查询本人所属机构及以下数据信息的参数信息为:"+JSONObject.toJSONString(ctrLoanContVO));
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();
		try {
			List<CtrLoanCont> list = ctrLoanContDao.queryByCurrDownOrgByPage(ctrLoanContVO);
			if(Objects.nonNull(list)) {
				for(CtrLoanCont cont : list) {
					CtrLoanContVO contVO = new CtrLoanContVO();
					beanCopy(cont, contVO);
					retList.add(contVO);
				}
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ retList.size());
			pageSet(retList, ctrLoanContVO);
			list = (List<CtrLoanCont>) beansCopy(retList, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return retList;
	}

	@Override
	public int updateContStatusByContNos(List<String> contNos, String contStatus) {
		logger.info("更新合同状态为["+contStatus+"]，合同号：" + JSONObject.toJSONString(contNos));
		return ctrLoanContDao.updateContStatusByContNos(contNos, contStatus);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CtrLoanContVO> queryCusListBySydInfo() {
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();
		try {
			List<CtrLoanCont> list = ctrLoanContDao.queryCusListBySydInfo();
			if(Objects.nonNull(list)) {
				for(CtrLoanCont cont : list) {
					CtrLoanContVO contVO = new CtrLoanContVO();
					beanCopy(cont, contVO);
					retList.add(contVO);
				}
			}
			list = (List<CtrLoanCont>) beansCopy(retList, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return retList;
	}

	@Override
	public CtrLoanContVO queryFirstCtrLoanCont(CtrLoanContVO ctrLoanContVO) {
		if(Objects.isNull(ctrLoanContVO)) {
			return null;
		}
		CtrLoanContVO ctrLoanContVOFromDB = new CtrLoanContVO();
		CtrLoanCont ctrLoanCont = ctrLoanContDao.queryFirstCtrLoanCont(ctrLoanContVO);
		ctrLoanContVOFromDB = beanCopy(ctrLoanCont, ctrLoanContVOFromDB);
		return ctrLoanContVOFromDB;
	}

	@Override
	public int getContSignedCountBySerno(String serno) throws Exception {
		return ctrLoanContDao.getContSignedCountBySerno(serno);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CtrLoanContVO> querySydListByCertcode(CtrLoanContVO ctrLoanContVO) {

		logger.info("当前查询税易贷数据信息的参数信息为:"+JSONObject.toJSONString(ctrLoanContVO));
		List<CtrLoanContVO> retList = new ArrayList<CtrLoanContVO>();
		try {
			List<CtrLoanCont> list = ctrLoanContDao.querySydListByCertcode(ctrLoanContVO);
			if(Objects.nonNull(list)) {
				for(CtrLoanCont cont : list) {
					CtrLoanContVO contVO = new CtrLoanContVO();
					beanCopy(cont, contVO);
					retList.add(contVO);
				}
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ retList.size());
			pageSet(retList, ctrLoanContVO);
			list = (List<CtrLoanCont>) beansCopy(retList, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return retList;
	}

	@Override
	public CtrLoanContVO queryContNo(CtrLoanContVO ctrLoanContVO) {
		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(ctrLoanContVO));
		try {
			CtrLoanCont queryCtrLoanCont = new CtrLoanCont();
			beanCopy(ctrLoanContVO, queryCtrLoanCont);
			CtrLoanCont queryRslCtrLoanCont = ctrLoanContDao.queryContNo(queryCtrLoanCont);
			if (Objects.nonNull(queryRslCtrLoanCont)) {
				CtrLoanContVO outCtrLoanContVo = beanCopy(queryRslCtrLoanCont, new CtrLoanContVO());
				logger.debug("当前查询结果为:"+ outCtrLoanContVo.toString());
				return outCtrLoanContVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CtrLoanContVO> queryAllByCusId(CtrLoanContVO ctrLoanContVO) {
		List<CtrLoanContVO> list = null;
		logger.info("查询数据信息的参数信息为:"+JSON.toJSONString(ctrLoanContVO));
		CtrLoanCont ctrLoanCont = beanCopy(ctrLoanContVO, new CtrLoanCont());
		try {
			List<CtrLoanCont> ctrLoanConts = ctrLoanContDao.queryAllByCusIdByPage(ctrLoanCont);
			logger.info("通过客户编号查询数据信息的结果集数量为:"+ ctrLoanConts.size());
			pageSet(ctrLoanConts, ctrLoanCont);
			list = (List<CtrLoanContVO>) beansCopy(ctrLoanConts, CtrLoanContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public CtrLoanContVO qryBySernoAndContTyp(CtrLoanContVO ctrLoanContVO) {
		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(ctrLoanContVO));
		CtrLoanContVO result = new CtrLoanContVO();
		try {
			CtrLoanCont queryCtrLoanCont = new CtrLoanCont();
			beanCopy(ctrLoanContVO, queryCtrLoanCont);
			queryCtrLoanCont = ctrLoanContDao.qryBySernoAndContTyp(queryCtrLoanCont);
			if (Objects.nonNull(queryCtrLoanCont)) {
				logger.debug("当前查询结果为:" + JSON.toJSONString(queryCtrLoanCont));
				result = beanCopy(queryCtrLoanCont, result);
			} else {
				logger.debug("通过业务流水号、合同类型查询到的合同信息为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return result;
	}

	/**
	 *
	 * 插入两条合同数据<br>
	 * 0.2:wanglihua:2019年5月5日 下午4:32:02:TODO <br>
	 * @author wanglihua
	 * @since 2019年5月5日 下午4:32:02
	 * @date 2019年5月5日 下午4:32:02
	 * @version 0.1
	 */
	@Override
	public int insertCtrLoanContList(List<CtrLoanContVO> ctrLoanContVOs) {
		logger.info("批量更新代偿状态开始！参数：" + ctrLoanContVOs);
		int count = 0;
		try {
			@SuppressWarnings("unchecked")
			List<CtrLoanCont> ctrLoanConts = (List<CtrLoanCont>) beansCopy(
					ctrLoanContVOs, CtrLoanCont.class);
			count = ctrLoanContDao.insertCtrLoanContList(ctrLoanConts);
			logger.info("批量更新成功！更新条数：" + count);
		} catch (Exception e) {
			logger.debug("批量更新代偿状态异常！" + e.getMessage());
		}
		return count;
	}
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateCtrLoanCont(List<CtrLoanContVO> list) {
		// TODO Auto-generated method stub
		LocalDate today = LocalDate.now();
		String msg1 = "【合同信息明细表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<CtrLoanCont> ctrLoanContVOlList = new ArrayList<CtrLoanCont>();
		int num = 0;
		try {
			ctrLoanContVOlList = (List<CtrLoanCont>) beansCopy(list, CtrLoanCont.class);
			num = ctrLoanContDao.insertOrUpdateCtrLoanCont(ctrLoanContVOlList);
			String num_msg = "【合同信息明细表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【合同信息明细表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int batchUpdate(List<CtrLoanContVO> list) {
		// TODO Auto-generated method stub
		LocalDate today = LocalDate.now();
		String msg1 = "【批量更新贷款合同表ctr_loan_cont】批量更新开始..." + today;
		logger.info(msg1, "message{}");
		List<CtrLoanCont> ctrLoanContVOlList = new ArrayList<CtrLoanCont>();
		int num = 0;
		try {
			ctrLoanContVOlList = (List<CtrLoanCont>) beansCopy(list, CtrLoanCont.class);
			num = ctrLoanContDao.batchUpdate(ctrLoanContVOlList);
			String num_msg = "【批量更新贷款合同表ctr_loan_cont】处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【批量更新贷款合同表ctr_loan_cont】出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
	/**
	 *
	 * 签章明细登记信息查询<br>
	 * @author wanglihua
	 * @since 2019年5月7日 上午9:32:32
	 * @date 2019年5月7日 上午9:32:32
	 * @version 0.1
	 */
	@Override
	public CtrLoanContVO queryInfosByPk(CtrLoanContVO ctrLoanContVO) {
		logger.debug("当前查询参数信息为:"+ JSON.toJSONString(ctrLoanContVO));
		try {
			CtrLoanCont queryCtrLoanCont = new CtrLoanCont();
			beanCopy(ctrLoanContVO, queryCtrLoanCont);
			CtrLoanCont queryRslCtrLoanCont = ctrLoanContDao.queryInfosByPk(queryCtrLoanCont);
			if (Objects.nonNull(queryRslCtrLoanCont)) {
				CtrLoanContVO outCtrLoanContVo = beanCopy(queryRslCtrLoanCont, new CtrLoanContVO());
				logger.debug("当前查询结果为:"+ outCtrLoanContVo.toString());
				return outCtrLoanContVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

//	@author:liuty
//	@date:2019/10/17
//	涉及产品:税e贷
//	功能:客户经理变更
//	原因:增量投产只投了接口没投实现，编译报错因此注释，不影响当前产品实际业务

//	@Override
//	public boolean batchUpdateCusManage(List<SedSynCusManageVO> sedSynCusManageVOList){
//		logger.info("批量更新客户经理开始" );
//		boolean result = false;
//		try {
//			List<SedSynCusManageVO> list = new ArrayList<SedSynCusManageVO>();//
//			int num = ctrLoanContDao.batchUpdateCusManage(sedSynCusManageVOList);
//			result =true;
//		} catch (Exception e) {
//			e.printStackTrace();
//			logger.error("客户经理变更更新台账表出错!", e);
//		}
//		return result;
//	}
	/**
	 *
	 * 查询合同号做条件进行签章明细登记信息查询<br>
	 * @author wanglihua
	 * @since 2019年5月7日 上午9:32:32
	 * @date 2019年5月7日 上午9:32:32
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CtrLoanContVO> queryContNo4Condition(CtrLoanContVO ctrLoanContVO) {
		List<CtrLoanContVO> list = null;
		logger.info("查询数据信息的参数信息为:"+JSON.toJSONString(ctrLoanContVO));
		CtrLoanCont ctrLoanCont = beanCopy(ctrLoanContVO, new CtrLoanCont());
		try {
			List<CtrLoanCont> ctrLoanConts = ctrLoanContDao.queryContNo4Condition(ctrLoanCont);
			logger.info("通过客户编号查询数据信息的结果集数量为:"+ ctrLoanConts.size());
			if (ctrLoanConts.size() > 0) {
				pageSet(ctrLoanConts, ctrLoanCont);
				list = (List<CtrLoanContVO>) beansCopy(ctrLoanConts, CtrLoanContVO.class);
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 *
	 * 授信合同查询<br>
	 * @author wanglihua
	 * @since 2019年5月7日 下午3:38:31
	 * @date 2019年5月7日 下午3:38:31
	 * @version 0.1
	 */
	@Override
	public List<CtrLoanContVO> queryCreditContByPage(CtrLoanContVO ctrLoanContVO) {
		List<CtrLoanContVO> list = new ArrayList<CtrLoanContVO>();
		try {
			//查询贷款合同表，得到授信合同
			CtrLoanCont queryctrLoanCont = new CtrLoanCont();
			List<CtrLoanContVO> queryRsCtrLoanConts = ctrLoanContDao.queryCreditContByPage(queryctrLoanCont);
			pageSet(queryRsCtrLoanConts, ctrLoanContVO);
			logger.debug("当前查询数据信息的结果集数量为:"+ queryRsCtrLoanConts.size());
			if (queryRsCtrLoanConts != null && !queryRsCtrLoanConts.isEmpty()) {
				for (CtrLoanContVO queryRsCtrLoanContsItem : queryRsCtrLoanConts) {
					CtrLoanContVO info = new CtrLoanContVO();
					info.setSerno(queryRsCtrLoanContsItem.getSerno());//授信申请号
					info.setContNo(queryRsCtrLoanContsItem.getContNo());//授信合同号
					info.setChannelCode(queryRsCtrLoanContsItem.getChannelCode());//渠道
					info.setPrdCode(queryRsCtrLoanContsItem.getPrdCode());//产品编号
					info.setPrdName(queryRsCtrLoanContsItem.getPrdName());//产品名称
					info.setCusId(queryRsCtrLoanContsItem.getCusId());//客户编号
					info.setCusName(queryRsCtrLoanContsItem.getCusName());//客户名称
					info.setCertType(queryRsCtrLoanContsItem.getCertType());//证件类型
					info.setCertCode(queryRsCtrLoanContsItem.getCertCode());//证件号码
					info.setFrzAmt(queryRsCtrLoanContsItem.getFrzAmt());//冻结额度
					info.setContState(queryRsCtrLoanContsItem.getContState());//合同状态
					info.setContNo(queryRsCtrLoanContsItem.getContNo());//授信合同编号
					info.setCusId(queryRsCtrLoanContsItem.getCusId());//客户编号
					info.setCusName(queryRsCtrLoanContsItem.getCusName());//客户名称
					info.setCertType(queryRsCtrLoanContsItem.getCertType());//证件类型
					info.setCertCode(queryRsCtrLoanContsItem.getCertCode());//证件号码
					info.setAssureMeansMain(queryRsCtrLoanContsItem.getAssureMeansMain());//担保方式
//						info.set(queryRsCtrLoanContsItem.get());//受理机构
//						info.set(queryRsCtrLoanContsItem.get());//出账机构
//						info.set(queryRsCtrLoanContsItem.get());//主办信贷员
					info.setChannelCode(queryRsCtrLoanContsItem.getChannelCode());//渠道名称
					info.setPrdName(queryRsCtrLoanContsItem.getPrdName());//产品名称

					//查询授信协议表，得到部分字段值
//					LmtPrdContVO lmtPrdContVO = new LmtPrdContVO();
//					lmtPrdContVO.setLmtContNo(queryRsCtrLoanContsItem.getLmtContNo());
//					LmtPrdContVO queryRsLmtPrdCont = lmtPrdContService.queryByPk(lmtPrdContVO);
//					if(Objects.isNull(queryRsLmtPrdCont)) {
//						info.setAvailAmt(queryRsLmtPrdCont.getAvailAmt());//可用额度
//						info.setOccAmt(queryRsLmtPrdCont.getOccAmt());//已用额度
//						info.setLmtAmt(queryRsLmtPrdCont.getLmtAmt());//授信总额度
//						info.setStartDate(queryRsLmtPrdCont.getStartDate());//授信起始日
//						info.setTerm(queryRsLmtPrdCont.getTerm());//授信期限
//						info.setExpireDate(queryRsLmtPrdCont.getExpireDate());//授信到期日
//						info.setCyclicFlg(queryRsLmtPrdCont.getCyclicFlg());//是否循环
//					} else {
//						logger.info("查询授信协议表结果为空！");
//					}

					//查询额度冻结申请表，得到冻结额度
//					LmtFreezeAppVO lmtFreezeAppVO = new LmtFreezeAppVO();
//					lmtFreezeAppVO.setFrzSerno(queryRsCtrLoanContsItem.getSerno());
//					LmtFreezeAppVO queryRsLmtFreezeApp = lmtFreezeAppService.queryByPk(lmtFreezeAppVO);
//					if(Objects.isNull(queryRsLmtFreezeApp)) {
//						info.setFrzAmt(queryRsLmtFreezeApp.getFrzAmt());
//					} else {
//						logger.info("查询额度冻结申请表结果为空！");
//					}
					list.add(info);
				}
			} else {
				logger.info("查询贷款合同表结果为空！");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	// 客户经理变更
	@Override
	public boolean batchUpdateCusManage(List<SedSynCusManageVO> sedSynCusManageVOList){
		logger.info("批量更新客户经理开始" );
		boolean result = false;
		try {
			List<SedSynCusManageVO> list = new ArrayList<SedSynCusManageVO>();//
			int num = ctrLoanContDao.batchUpdateCusManage(sedSynCusManageVOList);
			result =true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("客户经理变更更新台账表出错!", e);
		}
		return result;
	}

	@Override
	public CtrLoanContVO queryOneCtrData(String serno) {
		logger.info("查询条件为：" + serno);
		try {
			CtrLoanCont queryRsCtrLoanCont = new CtrLoanCont();
			queryRsCtrLoanCont = ctrLoanContDao.queryOneCtrData(serno);
			if (Objects.nonNull(queryRsCtrLoanCont)) {
				CtrLoanContVO outCtrLoanContVo = beanCopy(queryRsCtrLoanCont, new CtrLoanContVO());
				logger.debug("当前查询结果为:"+ outCtrLoanContVo.toString());
				return outCtrLoanContVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据转换出现异常!", e);
		}
		return null;
	}

	@Override
	public List<CtrLoanContVO> queryIncrementalData(String batchDate) {
		//查询符合条件的产品
		PrdInfoVO prdInfoVO = new PrdInfoVO();
		prdInfoVO.setIsUpload("01");
		List<PrdInfoVO> prdInfoVOS = prdInfoService.queryAllEligible(prdInfoVO);

		if (prdInfoVOS == null || prdInfoVOS.size() < 1) {
			logger.info("查询不到is_upload为01的产品数据");
			return null;
		}
		//根据产品查询数据
		List<CtrLoanContVO> list=this.ctrLoanContDao.queryIncrementalData(prdInfoVOS,batchDate);
		try {
			return (List<CtrLoanContVO>) beansCopy(list, CtrLoanContVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("转换异常",e);
		}
		return null;
	}

	@Override
	public CtrLoanContVO queryContNoByLmtContNoAndContype(String lmtContNo, String contType) {
		return beanCopy(this.ctrLoanContDao.queryContNoByLmtContNoAndContype(lmtContNo,contType),new CtrLoanContVO());
	}

	@Override
	public int updateStateByLmtContNo(CtrLoanContVO ctrLoanContVO) {
		CtrLoanCont ctrLoanCont = new CtrLoanCont();
		beanCopy(ctrLoanContVO,ctrLoanCont);
		return ctrLoanContDao.updateStateByLmtContNo(ctrLoanCont);
	}
}